package com.ml.mall.utils;

import java.math.BigDecimal;

/**
 * Created by kyyang on 2021/8/9 22:48
 */
public class OrderPriceUtils {


    /**
     * @description: type = 1 5分钱算法  type = 2 10分钱算法
     * 5分钱算法
     * 1.01~1.05 ==1.15  1.06~1.10 ==1.10
     * 10分钱算法
     * 1.01~1.10 ==1.10
     * @author kyyang
     * @date 2021/8/9 22:53
     * @version 1.0
     */
    public static BigDecimal getOrderPriceByCentType(int type,BigDecimal total){
       if(total==null||total.compareTo(BigDecimal.ZERO)<1){
            return BigDecimal.ZERO;
       }
        double result = total.doubleValue();
       //获取分位数
        int p = (int) (result*100%10);
        int j= (int) (result*10%10);
        result=result-(p/100.0);
        result=result-(j/10.0);


        //5分钱算法
        if(type==1){
            if(1<=p&&p<=5){
                p=5;
            }
            if(6<=p&&p<=9){
                p=0;
                j+=1;
            }
        }

        //10分钱算法
        if(type==2){
            if(1<=p&&p<=9){
                p=0;
                j+=1;
            }
        }

        result += (p/100.0);
        result += (j/10.0);

        return BigDecimal.valueOf(result).setScale(2,BigDecimal.ROUND_HALF_UP);
    }


    public static void main(String[] args) {
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.01)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.04)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.05)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.06)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.09)));
        System.out.println(getOrderPriceByCentType(1,BigDecimal.valueOf(1.10)));

        System.out.println("##################");
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.01)));
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.04)));
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.05)));
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.06)));
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.09)));
        System.out.println(getOrderPriceByCentType(2,BigDecimal.valueOf(1.10)));
    }


}
